package com.kerrykidz.system.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.parreserve.dao.model.ParRoomEntity;
import com.kerrykidz.parreserve.service.IParReserveService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.service.IParRoomService;
import com.kerrykidz.system.service.ISysBarCodeService;
import com.kerrykidz.system.util.UserUtil;
import com.rdp.framework.exception.RDPException;

@RequestMapping("ParRoomDetail")
@Controller
public class ParRoomController {
	@Autowired
	private IParRoomService iParRoomService;
	@Autowired
	private IParReserveService parReserveService;
	@Autowired
	private ISysBarCodeService iSysBarCodeService;
	@Autowired
	private ICommonService iCommonService;

	@RequestMapping("/ParRoom")
	public ModelAndView ParRoom() throws RDPException {
		String branId = UserUtil.getUserFromSession().getBranId();
		List<GblSecBranEntity> list = iSysBarCodeService.selectBranList(branId);
		ModelAndView mv = new ModelAndView("system/system_fanj");
		mv.addObject("list", list);
		mv.addObject("branId", branId);
		return mv;
	}

	@RequestMapping(value = "findRoomDetail", method = RequestMethod.POST)
	@ResponseBody
	public ParRoomEntity findRoomDetail(ParRoomEntity entity)
			throws RDPException {
		System.out.println(entity.getFkBranId());
		return iParRoomService.findRoomdetail(entity);

	}

	@RequestMapping("/deleteRoom")
	@ResponseBody
	public String deleteRoom(ParRoomEntity entity) throws RDPException,
			UnsupportedEncodingException {
		String modiBy = UserUtil.getUserFromSession().getUserId();
		entity.setModiBy(modiBy);
		iParRoomService.deleteRoomById(entity);
		return "success";
	}

	@RequestMapping("/addRoom")
	@ResponseBody
	public String addRoom(ParRoomEntity roomEntity) throws RDPException,
			UnsupportedEncodingException {
		String roomName = URLDecoder.decode(roomEntity.getRoomName(), "UTF-8");
		roomEntity.setRoomName(roomName);
		String modiBy = UserUtil.getUserFromSession().getUserId();
		List<ParRoomEntity> parRoomList = parReserveService
				.findParRoomOrderby();
		int orderIndex = 1;
		if (null != parRoomList && parRoomList.size() > 0) {
			orderIndex = parRoomList.get(0).getOrdIndex() + 1;
		}
		roomEntity.setOrdIndex(orderIndex);
		roomEntity.setPkRoomId(PrimaryKeyGenerator.getRandomPrimaryKey());
		roomEntity.setModiBy(modiBy);
		iParRoomService.addRoom(roomEntity);
		return "success";
	}

	@RequestMapping("/updateRoom")
	@ResponseBody
	public String updateRoom(ParRoomEntity roomEntity) throws RDPException,
			UnsupportedEncodingException {
		String roomName = URLDecoder.decode(roomEntity.getRoomName(), "UTF-8");
		String modiBy = UserUtil.getUserFromSession().getUserId();
		roomEntity.setRoomName(roomName);
		roomEntity.setModiBy(modiBy);
		iParRoomService.updateRoom(roomEntity);
		return "success";
	}
}
